package listaCustom.d3w.com;


import java.util.ArrayList;
import java.util.List;

import d3w.com.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class ListaCustom extends ArrayAdapter<Item> implements Filterable{
	private List<Item> items;
	private Filter filter= null;
	private final Object mLock = new Object();
    private ArrayList<Item> mOriginalValues;
    private LayoutInflater vi;

	public ListaCustom(Context context, int textViewResourceId, ArrayList<Item> objects) {
		super(context, 0, objects);
		items = objects;
		
		vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void addItemNormal(String nome){
		items.add(new ItemNormal(nome));
	};
	public void addSecao(String nome){
		items.add(new Secao(nome));
	};
	public Item get(int pos){
		return items.get(pos);
	};
	public int size(){
		return items.size();
	};
	
	public int getCount() {
        return items.size();  
    }

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;

		final Item i = items.get(position);
			if(i.isSection()){
				Secao si = (Secao)i;
				v = vi.inflate(R.layout.list_item_section, null);

				v.setOnClickListener(null);
				v.setOnLongClickListener(null);
				v.setLongClickable(false);
				
				final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
				sectionView.setText(si.getNome());
			}else{
				ItemNormal ei = (ItemNormal)i;
				v = vi.inflate(R.layout.list_item_entry, null);
				final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
				
				if (title != null) 
					title.setText(ei.getNome());
			}
		return v;
	}
	
	
	
	@Override
    public Filter getFilter()
    {
        if(filter == null)
            filter = new myFilter();
        return filter;
    }
    
    private class myFilter extends Filter{

    	@Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mOriginalValues == null) {
                synchronized (mLock) {
                    mOriginalValues = new ArrayList<Item>(items);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                synchronized (mLock) {
                    ArrayList<Item> list = new ArrayList<Item>(mOriginalValues);
                    results.values = list;
                    results.count = list.size();
                }
            } else {
                String prefixString = prefix.toString().toLowerCase();

                final ArrayList<Item> values = mOriginalValues;
                final int count = values.size();
                final ArrayList<Item> newValues = new ArrayList<Item>(count);

                for (int i = 0; i < count; i++) {
                    final Item value = values.get(i);
                    final String valueText = value.getNome().toLowerCase();

                    // First match against the whole, non-splitted value
                    if (value.isSection()) {
                    	newValues.add(value);
                    }
                    else {
                    	if (valueText.contains(prefixString)) {
                            newValues.add(value);
                        }	
                    }
                    
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @SuppressWarnings("unchecked")
		@Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
        	Log.v("count", results.count+"");
            items = (List<Item>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    	
    }

}
